package com.utilities.parsers.coordsParser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

import com.utilities.parsers.coordsParser.ParseEvents.EventTypes;

public class ParseCoordsFileParser   
{
	private LinkedList<ParseListener> eventListeners = new LinkedList<ParseListener>();
	
	public void addListener(ParseListener parseListener)
	{
		eventListeners.add(parseListener);
	}
	
	private void fireEvent(ParseEvents parseEvent)
	{
		for (ParseListener listener : eventListeners)
		{
			listener.handleEvent(parseEvent);
		}
	}
	
	public void parse(FileReader fileReader) throws IOException
	{
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		fireEvent(new ParseEvents(null,EventTypes.docStarted));
		String readLine = bufferedReader.readLine();
		while(readLine != null)
		{
			if (readLine.startsWith("[")) fireEvent(new ParseEvents(readLine,EventTypes.headerRead));
			else
				fireEvent(new ParseEvents(readLine, EventTypes.newEntry));
			readLine = bufferedReader.readLine();
		}
		fireEvent(new ParseEvents(null,EventTypes.docEnded));
	}
	
	public void parse(File fl) throws IOException
	{
		parse(new FileReader(fl));
	}
	
	public void removeAllListeners()
	{
		eventListeners.clear();
	}
	
	/**
	 * This method parses a non-commented line in a GFF3 file
	 * and returns the parameters in the form of a String array.
	 * param[0] = [S1] start of the alignment region in the reference sequence 
	 * param[1] = [E1] end of the alignment region in the reference sequence 
	 * param[2] = [S2] start of the alignment region in the query sequence 
	 * param[3] = [E2] end of the alignment region in the query sequence 
	 * param[4] = [LEN 1] length of the alignment region in the reference sequence 
	 * param[5] = [LEN 2] length of the alignment region in the query sequence 
	 * param[6] = [% IDY] percent identity of the alignment 
	 * param[7] = [% SIM] percent similarity of the alignment (as determined by the BLOSUM scoring matrix) 
	 * param[8] = [% STP] percent of stop codons in the alignment 
	 * param[9] = [LEN R] length of the reference sequence 
	 * param[10] = [LEN Q] length of the query sequence 
	 * param[11] = [COV R] percent alignment coverage in the reference sequence 
	 * param[12] = [COV Q] percent alignment coverage in the query sequence 
	 * param[13] = [FRM] reading frame for the reference and 
	 * param[14] = query sequence alignments respectively 
	 * param[15] = [TAGS] the reference and 
	 * param[16] = query FastA IDs respectively. 
	 * All output coordinates and lengths are relative to the forward strand of the reference DNA sequence.
	 * @param readLine
	 * @return
	 */
	public String[] parseCoordsFileLine(String readLine)
	{
		return readLine.split("\\t");
	}
	
	/**
	 * param[0] = [S1] start of the alignment region in the reference sequence 
	 * param[1] = [E1] end of the alignment region in the reference sequence 
	 * param[2] = [S2] start of the alignment region in the query sequence 
	 * param[3] = [E2] end of the alignment region in the query sequence 
	 * param[4] = [LEN 1] length of the alignment region in the reference sequence 
	 * param[5] = [LEN 2] length of the alignment region in the query sequence 
	 * param[6] = [% IDY] percent identity of the alignment 
	 * param[7] = [LEN R] length of the reference sequence 
	 * param[8] = [LEN Q] length of the query sequence 
	 * param[9] = [COV R] percent alignment coverage in the reference sequence 
	 * param[10] = [COV Q] percent alignment coverage in the query sequence 
	 * param[11] = [TAGS] the reference and 
	 * param[12] = query FastA IDs respectively.
	 * 
	 * @param readLine
	 * @return
	 */
	public String[] parseTilingFileLine(String readLine)
	{
		return readLine.split("\\t");
	}
}
